<?php

namespace app\index\validate;

use app\index\model\CertModel;
use app\index\model\DeptModel;
use app\index\model\UserModel;
use app\index\service\OptService;
use think\Validate;

class CertCompany extends Validate
{

    protected $rule = [
        'id' => 'require|number',
        'chanid' => 'require|check_chanid',
        'name' => 'require',
        'type' => 'require|check_type',
        'form' => 'require|number|check_form',
        'status' => 'require|number|check_status',
        'store_addr' => 'check_store_addr',
        'custodianid' => 'check_custodianid'
    ];

    protected $message = [
        'id' => '网络错误',
        'chanid' => '请选择所属单位',
        'name' => '请填写证书名称',
        'type' => '请选择证书分类',
        'form' => '请选择证书形式',
        'status' => '请选择证书状态'
    ];

    protected $scene = [
        'add' => ['chanid','name','type','form','status','store_addr','custodianid'],
        'edit' => ['id','chanid','name','type','form','status','store_addr','custodianid'],
    ];


    public function check_chanid($value){
        if(!DeptModel::where(['id'=>$value,'deleted_user'=>0])->find()){
            return '所属单位不存在，请重新选择';
        }
        return true;
    }

    public function check_type($value){
        if(!(new OptService())->check_option_exists('cert_type',$value)){
            return '证书分类不存在，请重新选择';
        }
        return true;
    }

    public function check_form($value){
        $form = CertModel::get_form();
        if(!isset($form[$value])) return '证书形式不存在，请重新选择';
        return true;
    }

    public function check_status($value){
        $status = CertModel::get_status();
        if(!isset($status[$value])) return '证书状态不存在，请重新选择';
        return true;
    }

    public function check_store_addr($value){
        if(!(new OptService())->check_option_exists('cert_store_addr',$value)){
            return '存放地不存在，请重新选择';
        }
        return true;
    }

    public function check_custodianid($value){
        if(!UserModel::where(['id'=>$value,'deleted_user'=>0])->find()){
            return '保管人不存在，请重新选择';
        }
        return true;
    }

}

